package _05多线程实现多人同时上传;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class UploadClient {
    public static void main(String[] args) throws IOException {

        for (int i = 0; i < 1; i++) {
            System.out.println("客户端启动");

            //创建socket对象
            Socket socket = new Socket("192.168.81.1",8888);

            //创建文件输入流
            FileInputStream fis = new FileInputStream("C:\\Users\\14863\\Desktop\\mac.jpg");

            //通过socket对象获取输出流,用于上传数据到服务器
            OutputStream socketOutputStream = socket.getOutputStream();

            //通过socket对象获取输入流，用于接收服务器回写数据
            InputStream socketInputStream = socket.getInputStream();

            //循环读取文件输入流中的数据并通过socket的输出流上传到服务器
            byte[] bytes = new byte[1024];
            int read;
            while ((read=fis.read(bytes))!=-1){
                socketOutputStream.write(bytes,0,read);
            }
            System.out.println("客户端上传完毕");
            //上传结束后，需要告诉服务器文件上传结束
            socket.shutdownOutput();

            //准备接收服务器回写数据
            while ((read=socketInputStream.read(bytes))!=-1){
                System.out.println(new String(bytes,0,read));
            }

            //关闭资源
            socketInputStream.close();
            socketOutputStream.close();
            fis.close();
            socket.close();
        }




    }
}
